var classify = function() {
};
classify.prototype = {};

classify.prototype.save = function(code, key, value, cb) {
    var self = this;
    code = code || "settings";
    if (typeof(value) != 'string') value = JSON.stringify(value);
    CMD.post("classify/save", {
        code: code,
        key: key,
        name: key,
        value: value,
    }, function(r) {
        self.refresh();
        if (cb) cb(r);
    });
}

classify.prototype.delete = function(id, cb) {
    CMD.post("classify/delete", {
        id: id
    }, cb);
}

classify.prototype.saveExt = function(code, key, value, cb) {
    var self = this;
    code = code || "settings";
    if (typeof(value) != 'string') value = JSON.stringify(value);
    CMD.post("classify/save", {
        code: code,
        key: key,
        name: key,
        value: '',
        ext: value
    }, function(r) {
        self.refresh();
        if (cb) cb(r);
    });
}

classify.prototype.query = function(code, key, convert, defaultValue, cb) {
    if (typeof(cb) != 'function') console.error("xxxx");
    var self = this;
    CMD.post("classify/query", {
        code: code || "settings",
        key: key,
        pageSize: 100,
    }, function(r) {
        if (isEmpty(r.list)) {
            cb(defaultValue);
            return;
        }
        r = r.list[0];
        if (!r.value || r.value == 'EMPTY') cb(defaultValue);
        else {
            if (convert) r.value = JSON.parse(r.value);
            cb(r.value);
        }
    })
}

classify.prototype.queryExt = function(code, key, convert, defaultValue, cb) {
    var self = this;
    CMD.post("classify/query", {
        code: code || "settings",
        key: key,
        pageSize: 100,
    }, function(r) {
        if (isEmpty(r.list)) {
            cb(defaultValue);
            return;
        }
        r = r.list[0];
        if (!r.ext || r.ext == 'EMPTY') cb(defaultValue);
        else {
            if (convert) r.ext = JSON.parse(r.ext);
            cb(r.ext);
        }
    });
}

classify.prototype.queryGroup = function(code, cb) {
    var self = this;
    CMD.post("classify/query", {
        code: code || "settings",
        pageSize: 100,
    }, function(r) {
        cb(r.list);
    })
}

classify.prototype.refresh = function() {
    CMD.post("classify/refresh", {});
}

window.Classify = new classify();
